const wx2my = require('../../wx2my');

const app = getApp();
Page({
  data: {
    onOff: 0,
    region: [],
    t_length: 0,
    sus: true,
    lng: 0,
    lat: 0,
    timer: null
  },
  onLoad: function () {},
  onReady: function () {
    this.data.timer = setInterval(() => {
      this.getLocation();
    }, 1000);
  },
  onUnload: function () {
    clearInterval(this.data.timer);
  },
  onShow: function () {
    this.get_phone();
  },

  get_phone() {
    let that = this;
    app.wxRequest('customer', {
      'type': 2
    }, function (res) {
      if (res.data.code == 1) {
        that.setData({
          tel: res.data.data.tel
        });
      }
    });
  },

  phone() {
    let that = this;
    wx2my.showModal({
      title: '温馨提示',
      content: '拨打电话:' + that.data.tel,

      success(res) {
        if (res.confirm) {
          wx2my.makePhoneCall({
            phoneNumber: that.data.tel
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }

    });
  },

  bindRegionChange: function (e) {

    my.regionPicker({
      customItems:["全国","全省","全市"],
      mergeOptions: {
        // 新增
        add: [
          {
            pid: '340000',
            id: 'x1',
            name: '新市',
            nextId: '340800',
            subList: [
              {
                name: '新区',
                id: 'x11',
              },
            ],
          },
        ],
        // 删除
        remove: [
          {
            id: '330000',
          },
        ],
        // 更新
        update: [
          {
            id: '110000',
            name: '北京',
            subList: [
              {
                name: '北京市',
                id: '110100',
                subList: [
                  {
                    name: '东城区',
                    id: '110101',
                  },
                ],
              },
            ],
          },
        ],
      },
      selectedItem: ['广东', '深圳', '福田区'],
      success: res => {
        console.log(res)

        
        this.setData({
          region: res.data
        });
      },
      fail: error =>{
        console.log(error)
      }
    });

    
    // this.setData({
    //   region: e.detail.value
    // });
  },

  /**
  * 调用wx.getLocation系统API,获取当前地理位置
  */
  getLocation: function () {
    var that = this; //获取位置

    wx2my.getLocation({
      type: 'gcj02',
      //默认为 wgs84 返回 gps 坐标，gcj02 返回可用于wx.openLocation的坐标
      success: function (res) {
        that.setData({
          postion: true
        });
        console.log(res); // 保存经纬度

        that.setData({
          lng: res.longitude,
          lat: res.latitude
        });
      },
      fail: function (e) {
        //首次弹窗提示授权，点击取消后，显示手动授权按钮
        that.setData({
          hideAuthBtn: false
        });
      }
    });
  },

  formSubmit(e) {
    console.log(e);

    if (this.data.onOff == 1) {
      return false;
    }

    ;
    let that = this;
    let res = e.detail.value;
    console.log(res);
    console.log(res);

    if (app.fm.isNull(res.name)) {
      app.toast('请输入您的姓名');
    } else if (app.fm.isNull(res.phone)) {
      app.toast('请输入您的手机号码');
    } else {
      that.setData({
        onOff: 1
      });
      app.wxRequest('collaborate_apply', {
        'token': wx2my.getStorageSync('token'),
        'name': res.name,
        'phone': res.phone,
        'city': that.data.region.join(",")
      }, function (res) {
        console.log(res);

        if (res.data.code == 1) {
          that.setData({
            sus: false
          });
          setTimeout(function () {
            wx2my.navigateBack();
          }, 2000);
        } else {
          that.setData({
            onOff: 0
          });
          app.toast(res.data.msg);
        }
      });
    }
  }

});